CFLAGS := -g -O0 -Wall

log:=thirdparty/log.h thirdparty/log.c
cjson:= thirdparty/cJSON.h thirdparty/cJSON.c

common:= app/app_common.h app/app_common.c

message:= app/app_message.h app/app_message.c

mqtt:= app/app_mqtt.h app/app_mqtt.c

pool:= app/app_pool.h app/app_pool.c

buffer:= app/app_buffer.h app/app_buffer.c

modbus:= app/app_modbus.h app/app_modbus.c

device := app/app_device.h app/app_device.c

curl:= ota/ota_http_curl.h ota/ota_http_curl.c

version:= ota/ota_version.h ota/ota_version.c

runner:= app/app_runner.h app/app_runner.c

daemon:= daemon/daemon_process.c daemon/daemon_process.h daemon/daemon_sub_process.c daemon/daemon_sub_process.h

test_log: test/test_log.c   $(log)
	gcc $(CFLAGS) -o $@ $^  -Ithirdparty
	#./$@
	#rm -f $@

test_common: test/test_common.c $(common) $(log)
	gcc $(CFLAGS) -o $@ $^  -Iapp -Ithirdparty
	./$@
	rm -f $@

test_message: test/test_message.c $(message) $(common) $(log) $(cjson)
	gcc $(CFLAGS) -o $@ $^  -Iapp -Ithirdparty
	./$@
	rm -f $@


test_mqtt: test/test_mqtt.c $(mqtt)   $(log)  
	gcc $(CFLAGS) -o $@ $^  -Iapp -Ithirdparty -lpaho-mqtt3c
	./test_mqtt
	rm -f $@

test_pool: test/test_pool.c   $(log) $(pool)
	gcc $(CFLAGS) -o $@ $^  -Iapp -Ithirdparty
	./$@
	rm -f $@

test_buffer: test/test_buffer.c   $(log) $(buffer)
	gcc $(CFLAGS) -o $@ $^  -Iapp -Ithirdparty
	./$@
	rm -f $@

test_modbus: test/test_modbus.c   $(log) $(modbus)
	gcc $(CFLAGS) -o $@ $^  -Iapp -Ithirdparty -lmodbus
	./$@
	rm -f $@

test_device: test/test_device.c   $(log) $(device) $(modbus) $(buffer)   $(cjson) $(pool) $(mqtt) $(common)
	gcc $(CFLAGS) -o $@ $^  -Iapp -Ithirdparty -lmodbus  -lpaho-mqtt3c
	./$@
	rm -f $@


test_curl: test/test_curl.c   $(log) $(curl)
	gcc $(CFLAGS) -o $@ $^  -Iota -Ithirdparty -lcurl
	./$@
	rm -f $@

test_version: test/test_version.c   $(log) $(curl) $(version) $(cjson)
	gcc $(CFLAGS) -o $@ $^  -Iota -Ithirdparty -lcurl -lcrypto
	./$@
	rm -f $@


gateway_x86: main.c $(runner) $(log) $(curl) $(version)   $(log) $(device) $(modbus) $(buffer)   $(cjson) $(pool) $(mqtt) $(common)
	gcc $(CFLAGS) -o $@ $^ -Iapp -Iota -Ithirdparty -lcurl -lcrypto -lmodbus  -lpaho-mqtt3c
	./$@ app
	rm -f $@

CROSS_COMPILE:=./toolchain/bin/arm-linux-gnueabihf-gcc 

SYSROOT:= --sysroot=./sysroot

all:=  $(runner) $(log) $(curl) $(version)   $(log) $(device) $(modbus) $(buffer)   $(cjson) $(pool) $(mqtt) $(common)
#all+= $(ota_http_curl) $(ota_version)
all+=  ${daemon}

all_lib:= -lpaho-mqtt3c  -lmodbus
all_lib+= -lcurl
all_lib+= -lcrypto

all_include := -Iapp -Ithirdparty
all_include += -Iota
all_include += -Idaemon

app_name := gateway

app_peer := root@192.168.43.71

clean:
	rm -f  gateway

cross_compile: main.c $(all)
	-$(CROSS_COMPILE) -o $(app_name) $^ $(all_lib) $(all_include) $(SYSROOT)
	-scp $(app_name)   $(app_peer):/usr/bin/$(app_name)
	rm -f  $(app_name)


init_shell:
	scp init/S99gateway   $(app_peer):/etc/init.d/S99gateway